package com.engine.kobelco.duss;

import com.alibaba.fastjson.JSONArray;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author dusongsong
 * @date 2023年10月31日 13:52
 */
public class KobelcoUtil extends BaseBean {

    public boolean isJSONArray(String str) {
        boolean flag = false;
        if (!(str.trim().equals(""))) {
            try {
                JSONArray.parseArray(str);
                flag = true;
            } catch (Exception e) {
                flag = false;
            }
        }
        return flag;
    }
    public String geIdByRequestid(String tablename,String requestid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from "+tablename+" where requestid=?",requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    //建模权限重构
    public static void newModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight(true);
        modeRightInfo.editModeDataShare(userid, formmodeid, id);
    }

    //将数据插入表单建模
    public static int insertModelData(String tablename, int userid, Map<String, Object> map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select a.id from modeinfo a left join workflow_bill b on a.formid=b.id where b.tablename=?";
        rs.executeQuery(sql, tablename);
        rs.next();
        int modeid = Util.getIntValue(rs.getString("id"));
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into " + tablename
                + "(modeuuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate(insSql, params)) {
            rs1.executeQuery("select id from " + tablename + " where modeuuid=?", uuid);
            rs1.next();
            dataid = Util.getIntValue(rs1.getString("id"));
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get(key).toString() + "',";
                }
                if (updatesql.endsWith(",")) {
                    updatesql = updatesql.substring(0, updatesql.length() - 1);
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构
                newModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }
    /**
     * 更新数据
     *
     * @param tablename 表名
     * @param map       数据集合
     * @param dataid    更新用唯一标识
     * @return
     */
    public boolean updateModelData(String tablename, int dataid, Map<String, Object> map) {
        RecordSet rs = new RecordSet();
        String updatesql = "update " + tablename + " set  ";
        String valuesStr = "";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            String value = Util.null2String(map.get(key).toString());
            if (valuesStr.equals("")) {
                valuesStr = key + "='" + value + "'";
            } else {
                valuesStr += "," + key + "='" + value + "'";
            }
        }
        updatesql += valuesStr + " where id='" + dataid + "'";
        writeLog(">>>>>updateModelData>>>>updatesql=" + updatesql);
        return rs.execute(updatesql);
    }

}
